<?php

//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                               |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: xiangminghu@clcw.com.cn                                        |
// +------------------------------------------------------------------------+
//
namespace AuctionBack\Controller;

use AuctionCore\CarResource\Car;
use AuctionCore\Finance\CarOwnerCompensate;
use AuctionCore\System\AuctionException;

/**
 * 车主补偿
 * @author Xmh
 */
class CarOwnerCompensateController extends BaseController
{

    private $CarOwnerCompensate;

    public function __construct()
    {
        parent::__construct();
        $this->emp_id = $this->user_info['emp_id'];

        //模板赋值
        $this->assign("emp_id", $this->emp_id);

        $this->CarOwnerCompensate = new CarOwnerCompensate();
    }

    public function index()
    {
        $branch_id = $this->user_info['branch_id'];
        //分公司列表
        $bc_list = $this->get_branch_company_list();
        $this->assign('bc_list', $bc_list);
        $this->assign('branch_id',$branch_id);
        $this->display();
    }
    /**
     * 获取车主补偿列表
     */
    public function compensate_list(){
        $page = I("get.p", 1);
        $size = 10;
        $params = array();
        $params['status'] = I('get.status');
        $params['location_area'] = I('get.location_area'); //所在地
        $params['keywords'] = I('get.keywords');

        //所在地查询
        if(count($this->bc_city)>1) {
            $bc_code = array_flip($this->bc_city);
            $bc_code = implode(',', $bc_code);
            $params['bc_city'] = $bc_code;
        }

        $result = $this->CarOwnerCompensate->get_car_owner_compensate_list($page, $size, $params);
        $list = empty($result['list']) ? array() : $result['list'];

        //分页
        $Page = new \AuctionCore\Common\AjaxPage($result['total'], $size, "get_page");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %nextPage% %downPage%  %end% %jumpform%");
//        dump($list);
        $show = $Page->show();          // 分页显示输出
        $this->assign('page', $show);   // 赋值分页输出
        $this->assign('list', $list);
        $this->assign('p', $page);
        $this->assign('params',$params);
        $this->display();
    }

    //处理
    public function compensate_deal(){
        $compensate_id = I('get.id/d',0);
        $compensate_detail = $this->CarOwnerCompensate->car_owner_compensate_detail($compensate_id);
        $compensate_status = $compensate_detail['compensate_status'];
        if($compensate_status > 1 || empty($compensate_status)){
            $this->redirect('CarOwnerCompensate/index','',1,'请求出错了');
        }
        $car_detail = \AuctionCore\CarResource\Car::getDetailWithChannel($compensate_detail['car_id']);
        $this->assign('car_detail', $car_detail);

        $show_from = \AuctionCore\Other\Channel::get_from();  //获取渠道来源信息
        $this->assign('show_from', $show_from);

        $transfer_type = C('TRANSFER_LIMIT');
        $this->assign('transfer_type', $transfer_type);

        $this->assign('detail',$compensate_detail);
        $this->display();
    }

    //确认补偿操作
    public function do_compensate_deal(){
        $compensate_id = I('post.compensate_id/d',0);
        $compensate_remark = I('post.compensate_remark');
        $data['id'] = $compensate_id;
        $data['emp_id'] = $this->emp_id;
        $data['remark'] = $compensate_remark;
        $data['status'] = 2;
        $res = $this->CarOwnerCompensate->save_car_owner_compensate($data);
        if(!$res){
            $this->helpJsonResult(400, '处理失败');
        }

        /**
         * 记录车辆跟踪日志
         */
        $car_owner_compensate_detail = $this->CarOwnerCompensate->get_car_owner_compensate($compensate_id,'car_id,order_id');
        $car_action_data = array(
            'action_name' => '车主补偿',
            'emp_id' => $this->emp_id
        );
        \AuctionCore\CarResource\Car::add_car_trace_log($car_owner_compensate_detail['car_id'], $car_action_data);
        /**
         * 记录拍品跟踪日志
         */
        $order_action_data = array(
            'action_name' => '车主补偿',
            'emp_id' => $this->emp_id
        );
        \AuctionCore\AuctionOrder\AuOrder::add_order_trace_log($car_owner_compensate_detail['order_id'], $order_action_data);

        $this->helpJsonResult(200, '处理成功');
    }

    //查看
    public function compensate_view(){
        $compensate_id = I('get.id/d',0);
        $compensate_detail = $this->CarOwnerCompensate->car_owner_compensate_detail($compensate_id);
        $compensate_status = $compensate_detail['compensate_status'];
        if($compensate_status < 2 || empty($compensate_status)){
            $this->redirect('CarOwnerCompensate/index','',1,'请求出错了');
        }
        $car_detail = \AuctionCore\CarResource\Car::getDetailWithChannel($compensate_detail['car_id']);
        $this->assign('car_detail', $car_detail);

        $show_from = \AuctionCore\Other\Channel::get_from();  //获取渠道来源信息
        $this->assign('show_from', $show_from);

        $transfer_type = C('TRANSFER_LIMIT');
        $this->assign('transfer_type', $transfer_type);


        $this->assign('detail',$compensate_detail);
        $this->display();
    }
}